<?php

namespace app\backend\controller;

use app\common\model\Coupon as couponModel;
use app\common\model\CouponCate;
use think\Controller;
use think\Validate;
use think\Request;
use think\facade\Env;

class Coupon extends Controller
{
    public function index()
    {
        return $this->fetch();
    }

    public function couponList()
    {
        $count = couponModel::count();
        $limit = request()->param('limit');
        $list = couponModel::alias('l')->Join('langjin_coupon_cate c','l.cat_id = c.id')->field('l.*,c.coupon_name')->order('l.id desc')->paginate($limit);
        foreach($list as $key=>$value){
            if($list[$key]['expired_time'] >0){
                $list[$key]['expired_time'] = date('Y-m-d H:i:s',$list[$key]['expired_time']);
            }
        }
        $data = ['code' => 0, 'msg' => '用户列表', 'count' => $count, 'data' => $list->items()];
        return json($data);

    }
    //优惠券分类
    public function cat_list()
    {
        return $this->fetch();
    }
    public function cateList()
    {
        $count = CouponCate::count();
        $limit = request()->param('limit');
        $list = CouponCate::order('id desc')->paginate($limit);
        $data = ['code' => 0, 'msg' => '用户列表', 'count' => $count, 'data' => $list->items()];
        return json($data);

    }


    /**
     *下架和上架商品
     **/
    public function caozuo_goods(Request $request){
        if($request->isPost()) {
            $id = $request->param('id');
            $res = couponModel::where(['id'=>$id])->find()->toArray();
            $info = couponModel::get($res['id']);
            $info['status'] = $info->getData('status');
            //print_r($res);exit;
            if(empty($res)){
                $data['status'] = 2;
                $data['msg'] = lang("出现异常");
            }else{
                if($info['status'] == '1'){
                    $data['status'] = 0;
                }else{
                    $data['status'] = 1;
                }
                $update = couponModel::where(['id'=>$id])->update($data);
                //echo goodsModel::getLastSql();exit;
                if($update){
                    $data['status'] = 1;
                    $data['msg'] = lang("操作成功");
                }else{
                    $data['status'] = 2;
                    $data['msg'] = lang("操作失败");
                }
            }
            return $data;
        }
    }
    /*
    * 添加优惠券分类
    */
    public function add_cate(Request $request)
    {
        return $this->fetch();
    }
    //操作
    public function doadd(Request $request)
    {
        $info = $_POST;
        $validate = new \app\common\validate\CouponCate;

        if (!$validate->check($info)) {
            $res['code'] = 0;
            $res['msg'] = $validate->getError();
            return $res;
        }
          $result = CouponCate::create($info);
         if($result){
            $data['code'] = 1;
            $data['msg'] = '添加成功';
          }else{
            $data['code'] = 0;
            $data['msg'] = '添加失败';
          }
          return $data;


    }
    //更新优惠券分类信息
    public function cat_edit(Request $request)
    { if (request()->isPost()){ //搜索
        $info = $_POST;
        $validate = new \app\common\validate\CouponCate;

        if (!$validate->check($info)) {
            $res['code'] = 0;
            $res['msg'] = $validate->getError();
            return $res;
        }
        $update = CouponCate::where('id', $info['id'])->update($info);
        if($update){
            $data['code'] = 1;
            $data['msg'] = '修改成功';
        }else{
            $data['code'] = 0;
            $data['msg'] = '修改失败';
        }
        return $data;
    }
        $id =  $request->param('id');
        if($id) {
            $info = CouponCate::where('id', $id)->find();
            $this->assign('info', $info);
            return $this->fetch();
        }
    }
    /*
* 添加优惠券
*/
    public function add_coupon(Request $request)
    {
        $select_info = CouponCate::all();
        $this->assign('select_info', $select_info);
        return $this->fetch();
    }
    //操作
    public function doadd_coupon(Request $request)
    {
        $info = $_POST;
        if($info['cat_id'] == 2){
            //如果优惠券类型为满减
            if(!$info['full_amount'] || $info['full_amount'] <=0 || $info['full_amount']<=$info['amount']){
                $data['code'] = 0;
                $data['msg'] = '满减价格不符合规则';
                return $data;
            }
        }
        if(!$info['amount'] || $info['amount'] <=0){
            $data['code'] = 0;
            $data['msg'] = '优惠券面值错误';
            return $data;
        }
        $info['expired_time'] = strtotime($info['expired_time']);
       // print_r($info);exit;
        $result = couponModel::create($info);
        if($result){
            $data['code'] = 1;
            $data['msg'] = '添加成功';
        }else{
            $data['code'] = 0;
            $data['msg'] = '添加失败';
        }
        return $data;


    }
    //更新优惠券分类信息
    public function coupon_edit(Request $request)
    { if (request()->isPost()){ //搜索
        $info = $_POST;
        if($info['cat_id'] == 2){
            //如果优惠券类型为满减
            if(!$info['full_amount'] || $info['full_amount'] <=0 || $info['full_amount']<=$info['amount']){
                $data['code'] = 0;
                $data['msg'] = '满减价格不符合规则';
                return $data;
            }
        }
        if(!$info['amount'] || $info['amount'] <=0){
            $data['code'] = 0;
            $data['msg'] = '优惠券面值错误';
            return $data;
        }
        $info['expired_time'] = strtotime($info['expired_time']);
        $update = couponModel::where('id', $info['id'])->update($info);
        if($update){
            $data['code'] = 1;
            $data['msg'] = '修改成功';
        }else{
            $data['code'] = 0;
            $data['msg'] = '修改失败';
        }
        return $data;
    }
        $id =  $request->param('id');
        if($id) {
            $info = couponModel::where('id', $id)->find();
            $select_info = CouponCate::all();
            $this->assign('select_info', $select_info);
            $this->assign('info', $info);
            return $this->fetch();
        }
    }

}
